package Mytest.day02;

import java.util.ArrayList;

//添加老师:通过键盘录入老师信息(id，name，age，birthday)
// 使用数组存储老师信息,要求老师的id不能重复
// 删除老师:通过键盘录入要删除老师的id值,将该老师从数组中移除,如果录入的id在数组中不存在,需要重新录 入
// 修改老师:通过键盘录入要修改老师的id值和修改后的老师信息 将数组中该老师的信息修改,如果录入的id在数组中不存在,需要重新录入
// 查询老师:将数组中存储的所有老师的信息输出到控制台
public class TeacherDao {
    static ArrayList<Teacher> list = new ArrayList<>();

   public static ArrayList<Teacher> findAllTeacher() {

       return list;
    }

    public static boolean deleteTeacher(String id) {

        for (int i = 0; i < list.size(); i++) {
            Teacher teacher = list.get(i);
            if (teacher.getId().equals(id)){
                list.remove(i);
            }
        }
        return true;
    }

    public static void updateTeacher(String id, Teacher newTeacher) {
        for (int i = 0; i < list.size(); i++) {

          if (newTeacher.getId().equals(id)) {
              list.set(i,newTeacher);
              break;
          }
        }


    }


    public boolean addTeacher(Teacher t) {
        int index = -1;

        for (int i = 0; i < list.size(); i++) {
            Teacher t1 = list.get(i);
            if (t1.getId().equals(t)) {
                index = i;
            }
        }
        if (index==-1){
            list.add(t);
            return true;
        }else {
            return false;
        }
    }
}
